/**
 * User-fiendly messaging interface
 */
var Log;

// Track errors in the queries
$(document).ajaxError(function (event, jqXHR, ajaxSettings, thrownError) {
	console.log (jqXHR);
	Log.warn ('A request to the <a href="' + ajaxSettings.url + '" target="_blank">WSS server</a> failed. Please ask for support to solve this issue.');
});

// Track pending queries
$(document).ajaxSuccess(function (event, XMLHttpRequest, ajaxOptions) {
	if (ajaxOptions.operation) {
		Log.inform ("Request to " + ajaxOptions.url + " has been completed.");
	}
});

// Splash screen support
function Desplash () {
	$(".splash", "#overlay").hide ("slow");
}

// Dynamic functionality
$(function () {
	
	// Configure the message bar
	Log = $("#msgbar").messages ();
	
	// Application seems to be loaded
	$("#error").hide ();
	$("#load").show ();
	
	// Load configuration data
	$.getJSON ("config.default.json", function (defaultConfig) {
		$.getJSON ("config.json", function (userConfig) {
			
			var config = $.extend (defaultConfig, userConfig);
			
			// Enable cross-domain requests
			jQuery.support.cors = config.remote;
			
			// Set default Web Services location
			SharePoint.Initialize (config.sharepoint);
			
			// Apply customization
			document.title = config.title;
			
			// Load platform applications
			Application.Load (config.extensions, config.extension);
			$("#load").hide ("slow", function () {
				if (Application.instances.length > 0) {
					console.log ("SharePoint Webtools finished loading");
					$("#success").show ("slow");
				} else {
					console.log ("SharePoint Webtools failed to load: no extensions initiated");
					$("#error").show ("slow");
				}
			});
			
			// Warn the user of the application's state
			Log.warn ("<b>Warning:</b> this application is still in its alpha version!");
		});
	});
});